package com.aaa.controller.goods;

import com.aaa.entity.Goods;
import com.aaa.service.GoodsService;
import com.aaa.service.impl.GoodsServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ChangeGoodsStstusServlet")
public class ChangeGoodsStstusServlet extends HttpServlet {
    GoodsService goodsService = null;

    @Override
    public void init() throws ServletException {
        goodsService = new GoodsServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsId = req.getParameter("goodsId");
        Integer status = IntegerUtils.ToInteger(req.getParameter("status"));

        Goods goods = new Goods();
        if(status==1){
            goods.setStatus(0);
        }else{
            goods.setStatus(1);
        }
        goods.setGoodsId(goodsId);

        int i = goodsService.changeStatus(goods);

        resp.getWriter().print(new Gson().toJson(goods));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
